/**
 * <pre>
 * ss-fire-collector / jobStateHandler.java
 * collector.httpd.metad.jobstate Class
 * jobStateHandler
 * <br>
 * 
 * <br>
 * @author ytjeong/apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 25.
 * </pre>
 */
package collector.httpd.metad.jobstate;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import collector.core.util.DateUtil;
import collector.core.util.JobTaskUtil;
import collector.core.util.StringUtil;
import collector.httpd.HttpdUtil;

/**
 * <pre>
 * Class : collector.httpd.metad.jobstate.jobStateHandler / jobStateHandler.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * 
 * @author ytjeong / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 25.
 */

public class JobStateHandler {
	private static Logger _log = LoggerFactory.getLogger(JobStateHandler.class);
//	private static final String _LogPrefixStr = CollectConstants.LOGGER_MODULE_NAME_SMC + CollectConstants.LOGGER_MODULE_NAME_AFTFIX;

//	private Config config = Config.newInstance();
//	private ConfigTask configTask = ConfigTask.newInstance();

	public void handle(String target, Request baseReq,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		baseReq.setQueryEncoding("utf-8");
		baseReq.setHandled(true);

		JobStateRequest reqvo = new JobStateRequest();
		HttpdUtil.bindRequestBean(reqvo, request);
		// _log.debug(reqvo);
		doit(target, baseReq, request, response, reqvo);
	}

	private void doit(String target, Request baseReq,
			HttpServletRequest request, HttpServletResponse response,
			JobStateRequest reqvo) throws IOException, ServletException {
		try {
			// procTest(request, response);
			execute(request, response, reqvo);
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
		}

	}

	private void execute(HttpServletRequest request,
			HttpServletResponse response, JobStateRequest reqvo)
			throws Exception {

		String title = ":: Job Status ::";
		String subtitle = "";
		String type = "";
		String useYN = "All";

		String timeFormat = "yyyy-MM-dd HH:mm:ss";
		long currtime = System.currentTimeMillis();
		if (StringUtil.isEmpty(reqvo.getJobIDs())) { // 작업ID 미존재시, 전체 대상
//			JobStateInfoData jsid = JobTaskUtil.loadJobStateData(reqvo.isUseYN());
			if (reqvo.isUseYN() == null) {
				type = "";
			} else { 
				if (reqvo.isUseYN().equals("true")) {
					type = "Y";
					useYN = "Use";
				} else if (reqvo.isUseYN().equals("false")) {
					type = "N";
					useYN = "Not Use";
				} else {
					type = "";
				}
			}
			subtitle = "All Job Module States :: " + " Display = " + useYN;// reqvo.getSrchCluster();
		} else { // 작업ID 존재시
			subtitle = "Single Job Module States :: " + reqvo.getJobIDs();
//			JobStateInfoData jsid = JobTaskUtil.loadJobStateData(reqvo.getJobIDs());
			type = reqvo.getJobIDs();
		}

		// TODO::get Log Contents
		String msg = "[2012-09-27 11:57:00] INFO   -     ModuleJob.execute(114)     | col-JMC-Log>	| ------------------------------------------------------------------------------------"
				+ "<br/>"
				+ "[2012-09-27 11:57:00] INFO   -     ModuleJob.execute(140)     | col-JMC-Log>	| ----- Complete to Excute Task [TX_LOGGING_01] -------------------"
				+ "<br/>"
				+ "[2012-09-27 11:57:28] DEBUG  - QuartzSchedulerThread.run(268)     | batch acquisition of 0 triggers";

		// msg =
		// net.apexcns.core.util.StringUtil.escapeHtmlBr(net.apexcns.core.util.StringUtil.escapeHtml(JobTaskUtil.makeModuleJobState("")));
		msg = JobTaskUtil.makeModuleJobState(type);

		response.setStatus(HttpServletResponse.SC_OK);
		// response.setContentLength(1024);
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");

		StringBuilder strb = new StringBuilder();
		strb.append("<h1>Report <b>" + title + "</b></h1>");
		strb.append("로드 시각 :: ").append(DateUtil.getDate(currtime, timeFormat)).append(" (").append(currtime).append(") miliseconds");

		strb.append("<h4>Title :: <b>" + subtitle + "</b></h4>");
		strb.append("" + msg + " ");

		strb.append("<br/>----- End of Text ----- ");
		response.getWriter().println(strb.toString());

		return;
	}

}
